﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using AutoLoL.Logic.Utils;
using AutoLoL.Logic.Windows;
using WindowsInput;
using WindowsInput.Native;
using Point = System.Drawing.Point;

namespace AutoLoL.Logic.Automation
{
    public class AutoLogin
    {
        public static bool Login(bool setFocus = false)
        {
            try
            {
                using (ClientWindow clientWindow = new ClientWindow())
                {
                    if (!clientWindow.IsForegroundWindow)
                    {
                        return false;
                    }

                    if (!clientWindow.IsLoginWindowOpen())
                    {
                        return false;
                    }

                    Thread.Sleep(100);

                    // Block user input
                    Win32.BlockInput(true);

                    // NOTE - MouseSimulator.MoveMouseTo() does not work when input is blocked!
                    KeyboardSimulator keyboardSimulator = new KeyboardSimulator();
                    MouseSimulator mouseSimulator = new MouseSimulator();

                    // Get Caps Lock state
                    bool isCapslockEnabled = (Win32.GetKeyState(Win32.VirtualKeyStates.VK_CAPITAL) == 1);

                    // Get current mouse position
                    System.Drawing.Point originalPosition;
                    Win32.GetCursorPos(out originalPosition);

                    // Click the login field
                    Point pointToClick = PointLocator.GetUsernameBox(clientWindow);
                    Win32.SetCursorPos(pointToClick.X, pointToClick.Y);
                    Thread.Sleep(50);
                    mouseSimulator.LeftButtonClick();
                    Thread.Sleep(50);

                    // Clear login field
                    keyboardSimulator.KeyDown(VirtualKeyCode.CONTROL);
                    keyboardSimulator.KeyPress(VirtualKeyCode.VK_A);
                    keyboardSimulator.KeyUp(VirtualKeyCode.CONTROL);
                    keyboardSimulator.KeyPress(VirtualKeyCode.DELETE);

                    // Write login name
                    string username = UserSettings.Instance.LolUsername;
                    if (isCapslockEnabled)
                    {
                        username = ReverseCase(username);
                    }

                    keyboardSimulator.TextEntry(username);

                    keyboardSimulator.KeyPress(VirtualKeyCode.TAB);

                    // Write password
                    string password = Encryption.Unprotect(UserSettings.Instance.LolPasswordEncrypted);
                    if (isCapslockEnabled)
                    {
                        password = ReverseCase(password);
                    }

                    keyboardSimulator.TextEntry(password);

                    keyboardSimulator.KeyPress(VirtualKeyCode.RETURN);

                    Win32.BlockInput(false);

                    return true;
                }
            }
            catch
            {
                Win32.BlockInput(false);
                if (Debugger.IsAttached) throw; 
            }

            return false;
        }

        private static string ReverseCase(string input)
        {
            return new string(input.Select(c => char.IsLetter(c) ? (char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c)) : c).ToArray());
        }
    }
}
